package common;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSONObject;

import java.util.HashSet;

import bean.UserInfo;
import share.LoginType;
import share.ShareUserInfo;

/**
 * Created by ilioili on 2014/12/21.
 */
public class UserConfig {
    public static final String SP_REQUEST_TOKEN = "request_token";
    public static final String SP_USER_SHARE_INFO_KEY = "UserInfo";
    public static final String SP_IS_LOGINED = "isLogined";
    public static final String SP_GETUI_ID = "getuiId";
    public static final String SP_IS_FIRST_USE = "IsFirstUse1";
    public static final String SP_USER = "user";
    final static String SP_BIND_BYPE = "bind_type";
    public static final String SP_PUSH_SWITCH = "is_push_on";
    public static final String SP_NIGHT_MODE = "night_mode";
    public static final String SP_MAIN_THEME = "main_theme";
    public static final String SP_FONT_SIZE = "font_size";
    public static final String SP_READED_TOPIC = "read_topic";
    public static final String SP_READED_NEWS = "read_news";
    public static final String SP_READED_ARTICLE = "read_article";
    public static final String SP_NO_PIC = "no_pic";
    private static ShareUserInfo shareUserInfo;
    private static UserInfo userInfo;
    private static SharedPreferences sp;

    public static void init(Context context) {
        if (null == sp) {
            sp = context.getSharedPreferences("UserConfig", Context.MODE_PRIVATE);
        }
    }

    public static String getRequestToken() {
        return sp.getString(SP_REQUEST_TOKEN, "");
    }

    public static void setRequestToken(String token) {
        sp.edit().putString(SP_REQUEST_TOKEN, token).commit();
    }

    public static LoginType getLoginType() {
        switch (sp.getInt(SP_BIND_BYPE, 0)) {
            case 1:
                return LoginType.QQ;
            case 2:
                return LoginType.WECHAT;
            case 3:
                return LoginType.SINA;
            default:
                return LoginType.NULL;
        }
    }

    public static void setLoginType(LoginType LoginType) {
        switch (LoginType) {
            case NULL:
                sp.edit().putInt(SP_BIND_BYPE, 0).commit();
                break;
            case QQ:
                sp.edit().putInt(SP_BIND_BYPE, 1).commit();
                break;
            case WECHAT:
                sp.edit().putInt(SP_BIND_BYPE, 2).commit();
                break;
            case SINA:
                sp.edit().putInt(SP_BIND_BYPE, 3).commit();
                break;
        }
    }

    public static boolean isLogined() {
        return sp.getBoolean(SP_IS_LOGINED, false);
    }

    public static void setIsLogin(boolean isLogin) {
        sp.edit().putBoolean(SP_IS_LOGINED, isLogin).commit();
    }

    public static boolean isNightMode() {
        return sp.getBoolean(SP_NIGHT_MODE, false);
    }

    public static void setNightMode(boolean isNightMode) {
        sp.edit().putBoolean(SP_NIGHT_MODE, isNightMode).commit();
    }

    public static int getMainTheme() {
        return sp.getInt(SP_MAIN_THEME, MyTheme.THEME_DEFAULT);
    }

    public static void setMainTheme(int themeId) {
        sp.edit().putInt(SP_MAIN_THEME, themeId).commit();
    }

    public static int getFontSize() {
        return sp.getInt(SP_FONT_SIZE, Constants.FONT_SIZE_NORMAL);
    }

    public static void setFontSize(int size) {
        sp.edit().putInt(SP_FONT_SIZE, size).commit();
    }

    public static UserInfo getUserInfo() {
        if (null == userInfo) {
            String userStr = sp.getString(SP_USER, "{}");
            userInfo = JSONObject.parseObject(userStr, UserInfo.class);
        }
        return userInfo;
    }

    public static void updateUserInfo(UserInfo u) {
        userInfo = u;
        sp.edit().putString(SP_USER, JSONObject.toJSONString(userInfo)).commit();
    }


    public static ShareUserInfo getShareUserInfo() {
        if (null == shareUserInfo) {
            String userStr = sp.getString(SP_USER_SHARE_INFO_KEY, "{}");
            shareUserInfo = JSONObject.parseObject(userStr, ShareUserInfo.class);
        }
        return shareUserInfo;
    }

    public static void clearSharedUserInfo() {
        shareUserInfo = null;
        sp.edit().remove(SP_USER_SHARE_INFO_KEY).commit();
    }

    public static void updateShareUserInfo(ShareUserInfo u) {
        shareUserInfo = u;
        sp.edit().putString(SP_USER_SHARE_INFO_KEY, JSONObject.toJSONString(shareUserInfo)).commit();
    }

    public static String getGetuiId() {
        return sp.getString(SP_GETUI_ID, "");
    }

    public static void setGetuiId(String cid) {
        sp.edit().putString(SP_GETUI_ID, cid).commit();
    }

    public static boolean isFirstUse() {
        return sp.getBoolean(SP_IS_FIRST_USE, true);
    }

    public static void setFirstUse(boolean b) {
        sp.edit().putBoolean(SP_IS_FIRST_USE, b).commit();
    }

    public static boolean isPushOn() {
        return sp.getBoolean(SP_PUSH_SWITCH, true);
    }

    public static void setIsPushOn(boolean b) {
        sp.edit().putBoolean(SP_PUSH_SWITCH, b).commit();
    }

    public static boolean isNoPic() {
        return sp.getBoolean(SP_NO_PIC, false);
    }

    public static void setIsNoPic(boolean b) {
        sp.edit().putBoolean(SP_NO_PIC, b).commit();
    }



    public static boolean isGuided(int guide) {
        return sp.getBoolean("user_guide_" + guide, false);
    }

    public static void setGuided(int guide) {
        sp.edit().putBoolean("user_guide_" + guide, true).commit();
    }

    public static boolean isReadedTopic(String topicId) {
        return sp.getStringSet(SP_READED_TOPIC, new HashSet<String>()).contains(topicId);
    }

    public static void setReadedTopic(String topicId) {
        HashSet<String> readedTopics = new HashSet<>(sp.getStringSet(SP_READED_TOPIC, new HashSet<String>()));
        readedTopics.add(topicId);
        sp.edit().putStringSet(SP_READED_TOPIC, readedTopics).commit();
    }

    public static boolean isReadedNews(String newsId) {
        return sp.getStringSet(SP_READED_NEWS, new HashSet<String>()).contains(newsId);
    }

    public static void setReadedNews(String newsId) {
        HashSet<String> readedNews = new HashSet<>(sp.getStringSet(SP_READED_NEWS, new HashSet<String>()));
        readedNews.add(newsId);
        sp.edit().putStringSet(SP_READED_NEWS, readedNews).commit();
    }

    public static boolean isReadedArticle(String articleId) {
        return sp.getStringSet(SP_READED_ARTICLE, new HashSet<String>()).contains(articleId);
    }

    public static void setReadedArticle(String articleId) {
        HashSet<String> readedArticles = new HashSet<>(sp.getStringSet(SP_READED_ARTICLE, new HashSet<String>()));
        readedArticles.add(articleId);
        sp.edit().putStringSet(SP_READED_ARTICLE, readedArticles).commit();
    }
}
